#include <kew/c-vm.h>
#include <kew/stdlib.h>


/* FIXME: this should finally raise a type conversion exception.  It isn't clear whether that should
 * allow a final conversion attempt to be made or not.
 * Maybe it could return an error object.
 */

KewObject *kew_check_type(KewDynamicEnvironment *env, KewStaticEnvironment *security_context, KewObject *object, char *informal_name, KewSelector conversion_selector)
{
    if (object->type == type)
        return object;
    
    if (kew_check_method(object, conversion_selector)) {
        KewObject *results[1];
        kew_send_full((ENV), (SECURITY_CONTEXT), (OBJECT), (CONVERSION_SELECTOR), KEW_CALL_SHAPE_01, NULL, &results, G_STRLOC);
    
        if (results[0]->type == type)
            return results[0];
        else
            g_error("Object @%s failed to return the correct type (%s) from a type conversion method", (OBJECT)->type.sourcepoint, (INFORMAL_NAME));
    }
    
    g_error("Object @%s could not be converted to type %s", object->type.sourcepoint, informal_name);
    return NULL; /* not reached */
}

gboolean kew_is_type(KewObjectType *type, KewObject *object)
{
    return object->type == type;
}
